use std::sync::{mpsc::{self, Receiver}, Arc, Mutex};

#[derive(Clone)]
pub struct SharedReceiver<T>(Arc<Mutex<Receiver<T>>>);

impl <T> Iterator for SharedReceiver<T>{
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.lock().unwrap().recv().ok()
    }
}

pub fn shared_channel<T>() -> (mpsc::Sender<T>, SharedReceiver<T>) {
    let (tx, rx) = mpsc::channel();
    (tx, SharedReceiver(Arc::new(Mutex::new(rx))))
}